//
//  UTEPedEventHandler.m
//  uteped_sdk
//
//  Created by mateng on 2024/2/19.
//

#import "UTEPedEventHandler.h"

@implementation UTEPedEventHandler

- (nonnull id)initWithMessenger:(nonnull NSObject<FlutterBinaryMessenger> *)messenger {
    if (self = [super init]) {
        self->eventChannel = [FlutterEventChannel eventChannelWithName:@"uteped_sdk/event" binaryMessenger:messenger];
        [self->eventChannel setStreamHandler:self];
    }
    
    return self;
}

- (FlutterError * _Nullable)onCancelWithArguments:(id _Nullable)arguments {
    self->eventSink = nil;
    return nil;
}

- (FlutterError * _Nullable)onListenWithArguments:(id _Nullable)arguments eventSink:(nonnull FlutterEventSink)events {
    self->eventSink = events;
    return nil;
}

- (void)publishEvent:(nonnull NSObject *)event async:(BOOL) async {
    if (async) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self->eventSink(event);
        });
    }
    else {
        self->eventSink(event);
    }
}

@end
